using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using FrameWork;
[CreateAssetMenu(menuName = "Data/State/Player/PlayerUseTool", fileName = "PlayerUseTool")]
public class PlayerState_UseTool : PlayerState
{
    float _stateStartTime;                                                         // 动画开始时间
    private string _stateName;                                                     // 动画名字
    [SerializeField,Range(0,1)] private float transitionDurationUseTool = 0.1f;    // 动画间过渡时间
    private int _stateHash;                                                        // 动画状态的Hash值
    private bool IsAnimationFinished => StateDuration >= animator.GetCurrentAnimatorStateInfo(0).length;          //当前动画是否播放完毕
    private float StateDuration => Time.time - _stateStartTime;                    // 动画播放的当前时间
    public override void Enter()
    {
        switch (Player.Instance.GetCurrentToolKit())
        {
            case PlayerToolkit.Attack: _stateName = "PlayerAttack";
                switch (PlayerManager.Instance.AttackPhase)
                {
                    case 1:animator.SetFloat("Attack", 0f);
                        break;
                    case 2:animator.SetFloat("Attack", 0.5f);
                        break;
                    case 0:animator.SetFloat("Attack", 1f);
                        break;
                }
                PlayerManager.Instance.AttackPhase = (PlayerManager.Instance.AttackPhase + 1) % 3;
                break;
            case PlayerToolkit.Axe: _stateName = "PlayerAxe";
                break;
            case PlayerToolkit.Hoe: _stateName = "PlayerHoe";
                break;
            case PlayerToolkit.Pickaxe: _stateName = "PlayerPickaxe";
                break;
            case PlayerToolkit.Watering: _stateName = "PlayerWatering";
                break;
        }
        _stateHash = Animator.StringToHash(_stateName);
        animator.CrossFade(_stateHash,transitionDurationUseTool);
        _stateStartTime = Time.time;
        
        // 触发使用工具
        EventSystem.Instance.EventTrigger(EventName.EVENT_PLAYER_ACTION_USE);
    }
    
    //
    public override void Exit()
    {
        base.Exit();
    }
    
    //
    public override void LogicUpdate()
    {
        base.LogicUpdate();
        // 动作结束
        if (IsAnimationFinished)
        {
            stateMachine.SwitchState(stateMachine.stateTable[typeof(PlayerState_Idle)]);
        }
        
        // 玩家受伤
        if (Player.Instance.IsHit)
        {
            stateMachine.SwitchState(stateMachine.stateTable[typeof(PlayerState_Hit)]);
        }

        // 玩家死亡
        if (Player.Instance.IsDeath)
        {
            stateMachine.SwitchState(stateMachine.stateTable[typeof(PlayerState_Death)]);
        }
    }
    
    //
    public override void PhysicalUpdate()
    {
        base.PhysicalUpdate();
    }
}
